#include "SettingsMenuState.h"
#include "Application.h"
#include "Fonts.h"
#include "Utils.h"
#include "Params.h"
#include <sfml/Graphics.hpp>

SettingsMenuState::SettingsMenuState()
{

	m_cNormal = sf::Color::White;
	m_cSelected = sf::Color::Yellow;
	m_cShadow = sf::Color::Color(50,50,50);

	sf::Font* font = Fonts::Istance()->GetFont("Consola");
	m_menu[0] = sf::Text("Match Time:", (*font), 26);//5 10 15
	m_menu[1] = sf::Text("Match Mode:", (*font), 26);//AI vs AI , P vs AI, AI vs P
	m_menu[2] = sf::Text("Input:", (*font), 26);//K/gamepad
	m_menu[3] = sf::Text("Screen Mode:", (*font), 26);//Window/Fullscreen
	m_menu[4] = sf::Text("Back", (*font), 26);

	m_shadows[0] = sf::Text("Match Time:", (*font), 26);
	m_shadows[1] = sf::Text("Match Mode:", (*font), 26);
	m_shadows[2] = sf::Text("Input:", (*font), 26);
	m_shadows[3] = sf::Text("Screen Mode:", (*font), 26);
	m_shadows[4] = sf::Text("Back", (*font), 26);

	m_value[0][0] = sf::Text("5 mins", (*font), 26);
	m_value[0][1] = sf::Text("10 mins", (*font), 26);
	m_value[0][2] = sf::Text("1,5 mins", (*font), 26);
	m_value[1][0] = sf::Text("AI vs AI", (*font), 26);
	m_value[1][1] = sf::Text("P vs AI", (*font), 26);
	m_value[1][2] = sf::Text("AI vs P", (*font), 26);
	m_value[2][0] = sf::Text("Keyboard", (*font), 26);
	m_value[2][1] = sf::Text("Gamepad", (*font), 26);
	m_value[2][2] = sf::Text("null", (*font), 26);
	m_value[3][0] = sf::Text("Window", (*font), 26);
	m_value[3][1] = sf::Text("Fullscreen", (*font), 26);
	m_value[3][2] = sf::Text("null", (*font), 26);

	m_currANDmax_value[0][0] = 0;
	m_currANDmax_value[0][1] = 2;
	m_currANDmax_value[1][0] = Params::MatchMode;
	m_currANDmax_value[1][1] = 2;
	m_currANDmax_value[2][0] = Params::InputMode;
	m_currANDmax_value[2][1] = 1;
	m_currANDmax_value[3][0] = Params::ScreenMode;
	m_currANDmax_value[3][1] = 1;

	float maxWidth = 0.f;
	float maxHeight = 0.f;

	for (int i  = 0; i < S_Elements ; i++)
	{
		maxWidth = (m_menu[i].GetRect().Width>maxWidth) ? m_menu[i].GetRect().Width : maxWidth;
		maxHeight = (m_menu[i].GetRect().Height>maxHeight) ? m_menu[i].GetRect().Height : maxHeight;
	}

	float maxValWidth = 0.f;
	float maxValHeight = 0.f;

	for (int i  = 0; i < S_Elements-1 ; i++)
	{
		for ( int j = 0; j < S_Value_Elements; j++)
		{
			maxValWidth = (m_value[i][j].GetRect().Width>maxWidth) ? m_value[i][j].GetRect().Width : maxWidth;
			maxValHeight = (m_value[i][j].GetRect().Height>maxHeight) ? m_value[i][j].GetRect().Height : maxHeight;
		}
	}

	m_posMenu = sf::Vector2f((Application::Istance()->Window()->GetWidth()*0.5f ) - (maxWidth*1.5f), 
								(Application::Istance()->Window()->GetHeight()*0.5f) - (maxHeight*0.5f*S_Elements));
	m_posValue = sf::Vector2f((Application::Istance()->Window()->GetWidth()*0.5f ) + (maxValWidth), 
								m_posMenu.y);
	
	for (int i  = 0; i < S_Elements-1 ; i++)
	{
		m_menu[i].SetPosition(sf::Vector2f(m_posMenu.x, 
											m_posMenu.y + maxHeight*i));
		m_shadows[i].SetPosition(sf::Vector2f(m_menu[i].GetPosition().x + 2.f, m_menu[i].GetPosition().y + 2.f));
		m_shadows[i].SetColor(m_cShadow);
		for (int j = 0; j < S_Value_Elements; j++)
			m_value[i][j].SetPosition(sf::Vector2f(
					m_posValue.x - (m_value[i][j].GetRect().Width)*0.5f, 
					m_posValue.y + maxValHeight*i));
	}
	m_menu[S_Elements-1].SetPosition(sf::Vector2f((Application::Istance()->Window()->GetWidth()*0.5f ) 
														- ((maxWidth-m_menu[S_Elements-1].GetRect().Width)*0.5f), 
												m_posMenu.y + maxHeight*S_Elements));
	m_shadows[S_Elements-1].SetPosition(sf::Vector2f(m_menu[S_Elements-1].GetPosition().x + 2.f, 
													m_menu[S_Elements-1].GetPosition().y + 2.f));
	m_shadows[S_Elements-1].SetColor(m_cShadow);


	m_selected = 0;
}

SettingsMenuState::~SettingsMenuState()
{
}

void SettingsMenuState::OnDraw(sf::RenderWindow& renderWindow)
{
	renderWindow.SaveGLStates();
	
	for (int i  = 0; i < S_Elements ; i++)
	{
		renderWindow.Draw(m_shadows[i]);
		m_menu[i].SetColor( ( i == m_selected) ? m_cSelected : m_cNormal );
		renderWindow.Draw(m_menu[i]);
		if (i != S_Elements-1)
		{
			m_value[i][m_currANDmax_value[i][0]].SetColor( ( i == m_selected) ? m_cSelected : m_cNormal );
			renderWindow.Draw(m_value[i][m_currANDmax_value[i][0]]);
		}
	}

	renderWindow.RestoreGLStates();
}

void SettingsMenuState::OnIdle(const sf::Input& input)
{
	if( Application::Istance()->IsInputAvailable() )
	{
		if (m_selected == S_Elements-1 //Back selected
			&& (input.IsKeyDown(sf::Key::Return) || input.IsJoystickButtonDown(0,2))) //'X' button or Enter pressed
		{
			Application::Istance()->SetState("Main");
			Application::Istance()->PauseInput(0.5f);
			m_selected = 0;
			SetParams();
		}
		else if (input.IsKeyDown(sf::Key::Down) || input.GetJoystickAxis(0,sf::Joy::AxisY)>=50.f )
		{
			m_selected = (m_selected == S_Elements-1) ? 0 : m_selected + 1 ;
			Application::Istance()->PauseInput(0.2f);
		}
		else if (input.IsKeyDown(sf::Key::Up) || input.GetJoystickAxis(0,sf::Joy::AxisY)<=-50.f )
		{
			m_selected = (m_selected == 0) ? S_Elements-1 : m_selected - 1 ;
			Application::Istance()->PauseInput(0.2f);
		}
		else if(m_selected < S_Elements - 1)
		{
			if (input.IsKeyDown(sf::Key::Right) || input.GetJoystickAxis(0,sf::Joy::AxisX)>=50.f )
			{
				(m_currANDmax_value[m_selected][0] >= m_currANDmax_value[m_selected][1]) ? 
					m_currANDmax_value[m_selected][0] = 0 : ++m_currANDmax_value[m_selected][0];

				Application::Istance()->PauseInput(0.3f);
			}
			else if (input.IsKeyDown(sf::Key::Left) || input.GetJoystickAxis(0,sf::Joy::AxisX)<=-50.f )
			{
				(m_currANDmax_value[m_selected][0] <= 0) ? 
					m_currANDmax_value[m_selected][0] = m_currANDmax_value[m_selected][1] : --m_currANDmax_value[m_selected][0];

				Application::Istance()->PauseInput(0.3f);
			}
		}
			
	}

}


void SettingsMenuState::SetParams()
{
	switch (m_currANDmax_value[0][0])
	{
		case 0:
			Params::GameTimeFactor = 18.f;
			break;
		case 1:
			Params::GameTimeFactor = 9.f;
			break;
		case 2:
			Params::GameTimeFactor = 60.f;
			break;
		default:
			break;
	}
	switch (m_currANDmax_value[1][0])
	{
		case 0:
			Params::MatchMode = Params::AIvsAI;
			break;
		case 1:
			Params::MatchMode = Params::PvsAI;
			break;
		case 2:
			Params::MatchMode = Params::AIvsP;
			break;
		default:
			break;
	}
	switch (m_currANDmax_value[2][0])
	{
		case 0:
			Params::InputMode = Params::Keyboard;
			break;
		case 1:
			Params::InputMode = Params::Gamepad;
			break;
		default:
			break;
	}
	/*switch (m_currANDmax_value[3][0])
	{
		case 0:
			{
				if (!Params::ScreenMode == Params::Window)
				{
					Params::ScreenMode = Params::Window;

					sf::VideoMode mode;
					mode.Width = 800;
					mode.Height = 600;
					mode.BitsPerPixel = 32;

					sf::ContextSettings settings;
					settings.DepthBits = 24;
					settings.StencilBits = 8;
					settings.AntialiasingLevel = 4;
					settings.MajorVersion = 3;
					settings.MinorVersion = 1;

					((sf::RenderWindow*)Application::Istance()->Window())->Create
						(mode,
						"AI Football",
						sf::Style::Close,
						settings);
					
					//m_window->UseVerticalSync(true);
					//m_window->SetFramerateLimit(0);
				}
				break;
			}
		case 1:
			{
				if (!Params::ScreenMode == Params::Fullscreen)
				{
					Params::ScreenMode = Params::Fullscreen;

					sf::VideoMode mode;
					mode.Width = 800;
					mode.Height = 600;
					mode.BitsPerPixel = 32;

					sf::ContextSettings settings;
					settings.DepthBits = 24;
					settings.StencilBits = 8;
					settings.AntialiasingLevel = 4;
					settings.MajorVersion = 3;
					settings.MinorVersion = 1;

					((sf::RenderWindow*)Application::Istance()->Window())->Create
						(mode,
						"AI Football",
						sf::Style::Fullscreen,
						settings);
					
					//m_window->UseVerticalSync(true);
					//m_window->SetFramerateLimit(0);
				}
				break;
			}
		default:
			break;
	}*/
}